//
//  RegionMemberTableViewCell.m
//  location
//
//  Created by Dongxiang Xu on 2025/5/21.
//

#import "RegionMemberTableViewCell.h"
#import "CashViewController.h"
#import "PopCashView.h"
#import "UIImageView+WebCache.h"

@interface RegionMemberTableViewCell ()

@property (nonatomic, strong) UIImageView *img;
@property (nonatomic, strong) UILabel *name;
@property (nonatomic, strong) UISwitch *arrive;
@property (nonatomic, strong) UISwitch *exit;
@property (nonatomic, strong) NSDictionary *location;
@property (nonatomic, strong) NSDictionary *groupInfo;

@end

@implementation RegionMemberTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = RealWhiteColor;
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(16, 8, SCREEN_WIDTH-32, 102)];
        view.layer.masksToBounds = YES;
        [view.layer setCornerRadius:16];
        view.backgroundColor = LightBGColor;
        [self.contentView addSubview:view];
        
        _img = [[UIImageView alloc] initWithFrame:CGRectMake(12, (view.height-56)/2, 56, 56)];
        _img.layer.masksToBounds = YES;
        [_img.layer setCornerRadius:22];
        [view addSubview:_img];
        
        _name = [[UILabel alloc] initWithFrame:CGRectMake(_img.maxX+16, 0, view.width-_img.maxX-126, view.height)];
        _name.textColor = RealBlackColor;
        _name.font = MEDIUM_FONT(14);
        [view addSubview:_name];
        
        UILabel *arrive = [[UILabel alloc] initWithFrame:CGRectMake(view.width-94, 17, 30, 24)];
        arrive.text = @"到达";
        arrive.font = DEFAULT_FONT(14);
        arrive.textColor = GraryLightColor;
        [view addSubview:arrive];
        
        _arrive = [[UISwitch alloc] initWithFrame:CGRectMake(arrive.maxX+8, 15, 42, 24)];
        [_arrive setFrame:CGRectMake(view.width-_arrive.width-12, arrive.y+(arrive.height-_arrive.height)/2, _arrive.width, _arrive.height)];
        [arrive setFrame:CGRectMake(_arrive.x-38, 17, 30, 24)];
        _arrive.onTintColor = NormalMainColor;
        _arrive.tintColor = RealWhiteColor;
        [_arrive addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
        [view addSubview:_arrive];
        
        UILabel *exit = [[UILabel alloc] initWithFrame:CGRectMake(view.width-94, CGRectGetMaxY(arrive.frame)+19, 30, 24)];
        exit.text = @"离开";
        exit.font = DEFAULT_FONT(14);
        exit.textColor = GraryLightColor;
        [view addSubview:exit];
        
        _exit = [[UISwitch alloc] initWithFrame:CGRectMake(exit.maxX+8, _arrive.maxY+14, 42, 24)];
        [_exit setFrame:CGRectMake(view.width-_exit.width-12, exit.y+(exit.height-_exit.height)/2, _exit.width, _exit.height)];
        [exit setFrame:CGRectMake(_exit.x-38, CGRectGetMaxY(arrive.frame)+19, 30, 24)];
        _exit.onTintColor = NormalMainColor;
        _exit.tintColor = RealWhiteColor;
        [_exit addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
        [view addSubview:_exit];
                       
    }
    return self;
}

- (void)loadWithInfo:(NSDictionary *)info
           GroupInfo:(NSDictionary *)groupInfo
            Location:(NSDictionary *)location
{
    _location = location;
    _groupInfo = groupInfo;
    [_img sd_setImageWithURL:[NSURL URLWithString:info[@"avatar"]]];
    _name.text = [NSString stringWithFormat:@"%@",info[@"nickName"]];
    if ([info[@"code"] integerValue]==4) {
        [_arrive setOn:NO animated:NO];
        [_exit setOn:NO animated:NO];
    }else if ([info[@"code"] integerValue]==3){
        [_arrive setOn:YES animated:NO];
        [_exit setOn:YES animated:NO];
    }else if ([info[@"code"] integerValue]==2){
        [_arrive setOn:NO animated:NO];
        [_exit setOn:YES animated:NO];
    }else{
        [_arrive setOn:YES animated:NO];
        [_exit setOn:NO animated:NO];
    }
}

- (void)switchAction:(UISwitch *)sender
{
    // 判断会员，黄金会员无限开启
    if ([self.groupInfo[@"membership"] isKindOfClass:[NSDictionary class]] && [self.groupInfo[@"membership"][@"planType"] integerValue]==3) {
        if (self.RegionMemberCellBlock) {
            if (_arrive.on && _exit.on) {
                self.RegionMemberCellBlock(3);
            }else if (!_arrive.on && _exit.on){
                self.RegionMemberCellBlock(2);
            }else if (_arrive.on && !_exit.on){
                self.RegionMemberCellBlock(1);
            }else if (!_arrive.on && !_exit.on){
                self.RegionMemberCellBlock(4);
            }
        }
    }else{
        if (self.location) {
            NSMutableDictionary *param = [NSMutableDictionary dictionary];
            [param setValue:self.location[@"id"] forKey:@"locationId"];
            CryptoHTTPKit *manager = [[CryptoHTTPKit alloc] init];
            [manager encryptedPOST:@"/system/group/checkLocationNotification"
            parameters:param
            success:^(NSDictionary *responseObject) {
                if ([responseObject[@"code"] integerValue]==200) {
                    if (self.RegionMemberCellBlock) {
                        if (self.arrive.on && self.exit.on) {
                            self.RegionMemberCellBlock(3);
                        }else if (!self.arrive.on && self.exit.on){
                            self.RegionMemberCellBlock(2);
                        }else if (self.arrive.on && !self.exit.on){
                            self.RegionMemberCellBlock(1);
                        }else if (!self.arrive.on && !self.exit.on){
                            self.RegionMemberCellBlock(4);
                        }
                    }
                }else{
                    if ([self.groupInfo[@"membership"] isKindOfClass:[NSDictionary class]]) {
                        UIAlertController * alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"最多开启%@个提醒（%@会员）",(([self.groupInfo[@"membership"][@"planType"] integerValue]==1)?@"10":@"20"),(([self.groupInfo[@"membership"][@"planType"] integerValue]==1)?@"亲密":@"白银")] message:@"" preferredStyle:UIAlertControllerStyleAlert];
                        UIAlertAction *act =[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        }];
                        [alert addAction:act];
                        [self.parentContainerViewController presentViewController:alert animated:YES completion:nil];
                    }else{
                        [sender setOn:NO animated:YES];
                        PopCashView *view = [[PopCashView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) GroupInfo:self.groupInfo];
                        [WINDOW addSubview:view];
                        view.PopCashBlock = ^{
                            CashViewController *vc = [[CashViewController alloc] init];
                            vc.groupInfo = self.groupInfo;
                            [self.viewContainingController.navigationController pushViewController:vc animated:YES];
                        };
                    }
                }
            }
            failure:^(NSError *aError) {
                
            }];
        }else{
            if (self.RegionMemberCellBlock) {
                if (self.arrive.on && self.exit.on) {
                    self.RegionMemberCellBlock(3);
                }else if (!self.arrive.on && self.exit.on){
                    self.RegionMemberCellBlock(2);
                }else if (self.arrive.on && !self.exit.on){
                    self.RegionMemberCellBlock(1);
                }else if (!self.arrive.on && !self.exit.on){
                    self.RegionMemberCellBlock(4);
                }
            }
        }
    }
    
}

@end
